package com.yuanfeng.controller;

import com.yuanfeng.basedata.annotation.ConfigManagementSwagger;
import com.yuanfeng.basedata.api.FineFoodConstantApi;
import com.yuanfeng.basedata.entiry.KcsjColumn;
import com.yuanfeng.basedata.entiry.KcsjFood;
import com.yuanfeng.basedata.entiry.KcsjMessage;
import com.yuanfeng.basedata.entiry.KcsjUser;
import com.yuanfeng.basedata.repository.KcsjColumnRepository;
import com.yuanfeng.basedata.response.ResponseResult;
import com.yuanfeng.service.FineFoodService;
import com.yuanfeng.service.MessageManageService;
import com.yuanfeng.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @ClassName MessageManageController
 * @Description T0D0
 * @Author yuanfeng
 * @Date 2019/9/7 10:14
 * @Version 1.0
 **/
@RequestMapping(FineFoodConstantApi.ADMIN_PATH)
@RestController
@Validated
@Api(description = "后台留言管理模块")
@ConfigManagementSwagger
public class MessageManageController {

    private static final Logger LOGGER = LoggerFactory.getLogger(MessageManageController.class);

    @Autowired
    private MessageManageService messageManageService;

    @ApiOperation(value = "显示所有的留言")
    @RequestMapping(value = "/all/messages", method = RequestMethod.POST)
    public ResponseResult<List<KcsjMessage>> showAllMessages() {

        ResponseResult<List<KcsjMessage>> responseResult = new ResponseResult<>();

        try {
            List<KcsjMessage> kcsjMessages = messageManageService.listAllMessages();
            if (kcsjMessages.size() > 0) {

                responseResult.setCode(200);
                responseResult.setMessage("查询所有留言成功");
                responseResult.setData(kcsjMessages);
            }
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.warn(e + "");
            responseResult.setCode(500);
            responseResult.setMessage("查询所有留言失败");
            responseResult.setData(null);
        }

        return responseResult;

    }

    @ApiOperation(value = "删除留言")
    @RequestMapping(value = "/delete/message", method = RequestMethod.DELETE)
    public ResponseResult<List<KcsjMessage>> deleteMessage(@ApiParam("留言ID") @RequestParam String messageId) {

        ResponseResult<List<KcsjMessage>> responseResult = new ResponseResult<>();


        try {
            if (StringUtils.isNotBlank(messageId)){
                int paramId = Integer.parseInt(messageId);
                messageManageService.deleteByMessageId(paramId);
                responseResult.setCode(200);
                responseResult.setMessage("删除留言成功");
                responseResult.setData(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.warn(e + "");
            responseResult.setCode(500);
            responseResult.setMessage("删除留言失败");
            responseResult.setData(null);
        }

        return responseResult;

    }


}
